package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import logica.loEspecialidades;
import logica.loPlanes;
import entidades.*;
import dataSet.*;

/**
 * Servlet implementation class ABMPlanes
 */
@WebServlet("/ABMPlanesServlet")
public class ABMPlanesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ABMPlanesServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);	
		
		String opcion = request.getParameter("opt");
		session.setAttribute("opcion", opcion);
		
		String id = (request.getParameter("id").toString());
		Plan planActual = new Plan();
		loPlanes lp = new loPlanes();
		
		if(!id.equals("null")){
			planActual = lp.getPlan(Integer.parseInt(id));
		
			session.setAttribute("planActual", planActual);
		}
		
		if (opcion.equalsIgnoreCase("editar") || opcion.equalsIgnoreCase("agregar")){
			recuperarEspecialidades(request, response);
			request.getRequestDispatcher("AltaModifPlan.jsp").forward(request, response);
		}
		else if (opcion.equalsIgnoreCase("borrar")){
			borrarPlan(Integer.parseInt(id), request, response);
			mostrarPlanes(response,request);
			request.getRequestDispatcher("ABMPlanes.jsp").forward(request, response);
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);	
		altaModifPlanes(request, response);
		session.removeAttribute("msg");
		mostrarPlanes(response,request);
		request.getRequestDispatcher("ABMPlanes.jsp").forward(request, response);
	}
	
	public void borrarPlan(int id, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		loPlanes lp = new loPlanes();
		HttpSession session = request.getSession(true);	
		
		if (lp.estaAsociado(request.getParameter("id"))){
            String msg = "El Plan esta asociado a Materias, Comisiones o a una Persona. No se puede eliminar.";
            session.setAttribute("msgPlan", msg);
        }
        else
        {
        	session.removeAttribute("msgPlan");
        	lp.borrarPlan(id);
        }
	}
	
	public void altaModifPlanes(HttpServletRequest request, HttpServletResponse response){
	 	Plan planActual = new Plan();
	 	loPlanes lp = new loPlanes();
	 		
	 	planActual.setDescripcion(request.getParameter("descripcion"));
	 	planActual.setIdEspeciliadad(Integer.parseInt(request.getParameter("idEsp")));
	 	String id = request.getParameter("id");
	 	if(id == null || id.isEmpty()){
	 	   lp.agregarPlan(planActual);
	 	}else{
	 	 	planActual.setId(Integer.parseInt(id));
	 	   	lp.modificarPlan(planActual);
	 	}
	}
	
	public void mostrarPlanes(HttpServletResponse response, HttpServletRequest request) throws IOException{
		loPlanes lp = new loPlanes();
		ArrayList<PlanEspecialidad> listaPlan = lp.recuperTodos();
		request.setAttribute("planesEsp", listaPlan);
	}
	
	public void recuperarEspecialidades(HttpServletRequest request, HttpServletResponse response){
		HttpSession session = request.getSession(true);	    

		loEspecialidades le = new loEspecialidades();
        ArrayList<Especialidad> especialidades = le.recuperTodas();
		session.setAttribute("especialidades", especialidades);
	}

}
